﻿@{
    ViewBag.Title = "配置系统权限";
    Layout = "~/Views/Shared/_LayoutKoJS.cshtml";
}

<div class="aux-body">
    <!--<h1 class="itemtitle">My Resource</h1> -->
    <div class="aux-tabcontent">
        <div class="fx-grid">
            <div id="fx-scrollbar" style="width: 900px;">
                <!-- style变化 height:650px; -->
                <div id="fx-scrollbar-scrollable" style="width: 950px;">
                    <!-- style变化 -->
                    <div class="fx-grid-container fx-scrollbar-content" style="overflow: visible; width: 900px;">
                        <!-- style变化 -->
                        <div class="quickStarttab" style="height: 500px">
                            <div id="Ticketfield">
                                <div class="quickStarttab-item" style="width: 100%;">
                                    <label style="width:10%">登录名：</label>
                                    <input type="text" name="loginID" id="loginID">
                                    <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="PermissionSearch" onclick="PermissionSearch()" style="margin-left:50px"><span class="ui-button-text">查询</span></button>
                                    <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" id="PermissionSave" onclick="PermissionSave()"><span class="ui-button-text">保存</span></button>
                                </div>
                            </div>
                            <div style="margin-top:20px">
                                <table id="gridTable1" style="width: 580px">
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

@section scripts {
<script type="text/javascript">
    $("#LayoutPermission").addClass("selected");
    $("#loginID").focus();
    $(document).ready(function () {
        $("#gridTable1").jqGrid({
            datatype: 'local',
            rowNum: 500,
            multiselect: true,
            colNames: ['ID','ShortDescription', '名称', 'LoginID', 'Disabled', 'GroupID', '权限类型','备注'],
            colModel: [
                { index: "1", name: "RequestID", key: true, sorttype: "text", hidden: true },
                { index: "8", name: "ShortDescription", sorttype: "text", hidden: true },
                { index: "105", name: "GroupName", sorttype: "text",width:60 },
                { index: "536870916", name: "LoginID", sorttype: "text", hidden: true },
                { index: "536870917", name: "Disabled", sorttype: "text", hidden: true },
                { index: "536870918", name: "GroupID", sorttype: "int", hidden: true },
                { index: "536870919", name: "IsArtechGroup", sorttype: "text",width:60,hidden:true },
                { index: "536870915", name: "FullName", sorttype: "text" },
            ],
            viewrecords: true,
            jsonReader: {
                repeatitems: false,
                id: "rule"
            },
            mtype: "GET",
            autowidth: true,
            sortname: 'RequestID',
            sortorder: 'asc',
            //postData: {
            //    Permission: 'public',
            //},
            //prmNames: {
            //    id: 'Type'
            //},
            scrollOffset: 0,
            height: "280",
            width: "500",
            viewrecords: true,
            gridview: true,

            onSelectRow: function (rowid, status, e, rowObject) {
            },
            ondblClickRow: function () {
            },
            gridComplete: function () {
            },
            loadComplete: function () {
            }
        });

        LoadHosts();
    })

    function LoadHosts() {
        var data = {};
        jQuery("#gridTable1").jqGrid("setGridParam", {
            url: "/API/PermissionAPI/GetTemplateList", //设置表格的url
            datatype: "json", //设置数据类型
            }).trigger("reloadGrid");

    }

    function PermissionSave() {
        var loginid = $("#loginID").val();
        if (!(loginid == "" || loginid == null)) {
            var loginname;
            $.ajax("/API/PermissionAPI/GetFullName?loginID=" + loginid, {
                type: "GET", contentType: "application/json",
                async: false,
                success: function (data) {
                    loginname = data;
                }
            });

            var CheckedList = [];
            $("#gridTable1 .cbox").each(function (index, element) {
                if (element.checked) {
                    var data = {};
                    data['ShortDescription'] = "Add";
                    data['GroupName'] = jQuery('#gridTable1').getRowData()[index].GroupName;
                    data['FullName'] = loginname;
                    data['LoginID'] = loginid;
                    data['Disabled'] = "No"
                    data['GroupID'] = jQuery('#gridTable1').getRowData()[index].GroupID;
                    data['IsArtechGroup'] = jQuery('#gridTable1').getRowData()[index].IsArtechGroup;
                    CheckedList.push(data);
                }
            });
            if (CheckedList.length > 0) {
                $.ajax("/API/PermissionAPI/loginid=" + '@ViewBag.loginname', {
                    data: JSON.stringify(CheckedList),
                    type: "POST", contentType: "application/json",
                    success: function (data) {
                        alert(data);
                    }
                });
            }
        }
        else { alert("请先输入查询登录名"); }
    }


    function PermissionSearch() {
        var loginid = $("#loginID").val();
        var datalist = [];
        if (!(loginid == "" || loginid == null)) {
            $("#gridTable1").resetSelection();
            $.ajax("/API/PermissionAPI/GetList?LoginName=" + loginid, {
                type: "GET", contentType: "application/json",
                async:false,
                success: function (data) {
                    datalist=data;
                }
            });
            for (var i = 0; i < datalist.length; i++) {
                if (datalist[i] != "") {
                    $($("#gridTable1 [title=" + datalist[i].GroupID + "]").parent().children()[0].children).prop("checked", true);
                    $("#gridTable1").setSelection($("#gridTable1 [title=" + datalist[i].GroupID + "]").parent().attr("id"), true);
                }
            }
        }
        else { alert("请先输入查询登录名"); }
    }
</script>
}